<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>{{ $route.query.id? '编辑':'添加' }}</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form ref="ruleForm" label-width="100px" :rules="rules" :model="addForm">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="法人" prop="legalPerson">
              <el-input v-model="addForm.legalPerson" />
            </el-form-item>
            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="addForm.registeredAddress" />
            </el-form-item>
            <el-form-item label="所在行业" prop="industryCode">
              <el-select v-model="addForm.industryCode">
                <el-option
                  v-for="item in options"
                  :key="item.industryCode"
                  :label="item.industryName"
                  :value="item.industryCode"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="企业联系人" prop="contact">
              <el-input v-model="addForm.contact" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" />
            </el-form-item>
            <el-form-item label="营业执照" prop="businessLicenseUrl">
              <el-upload action="#" :http-request="uploadRequest">
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
              </el-upload>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { getIndustryList, uploadAPI, addEnterprise, getEnterpriseInfo, updateEnterprise } from '@/api/park/ParkEnterprise'
export default {
  data() {
    return {
      addForm: {
        name: '', // 企业名称
        legalPerson: '', // 法人
        registeredAddress: '', // 注册地址
        industryCode: '', // 所在行业
        contact: '', // 企业联系人
        contactNumber: '', // 联系人电话
        businessLicenseUrl: '', // 营业执照url
        businessLicenseId: '' // 营业执照id
      },
      rules: {
        name: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        contact: [
          { required: true, message: '请输入企业联系人', trigger: 'blur' }
        ],
        contactNumber: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          // 校验手机号
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        businessLicenseUrl: [
          { required: true, message: '请上传营业执照', trigger: 'blur' }
        ],
        industryCode: [
          { required: true, message: '请选择所在行业', trigger: 'blur' }
        ],
        legalPerson: [
          { required: true, message: '请输入法人', trigger: 'blur' }
        ],
        registerdAddress: [
          { required: true, message: '请输入注册地址', trigger: 'blur' }
        ]
      },
      options: [
      ]
    }
  },
  created() {
    this.getIndustryList()
    this.$route.query.id && this.getEnterpriseInfo()
  },
  methods: {
    // 获取行业列表 下拉框
    async getIndustryList() {
      const data = await getIndustryList()
      //   console.log(data)
      this.options = data
    },
    // 上传营业执照
    async uploadRequest(params) {
    //   console.log(params)
      const { file } = params
      const formData = new FormData()
      formData.append('file', file)
      formData.append('type', 1)
      const { id, url } = await uploadAPI(formData)
      //   console.log(data)
      this.addForm.businessLicenseUrl = url
      this.addForm.businessLicenseId = id
      console.log(this.addForm)
    },
    // handleChange(file, fileList) {
    //   this.addForm.businessLicenseUrl = fileList
    //   this.refs.ruleForm.validateField('businessLicenseUrl') // 手动触发校验
    // },
    // 获取企业信息
    async getEnterpriseInfo() {
      const {
        name,
        id,
        legalPerson,
        registeredAddress,
        industryCode,
        businessLicenseUrl,
        businessLicenseId,
        contact,
        contactNumber
      } = await getEnterpriseInfo(this.$route.query.id)
      //   console.log(data)
      this.addForm = { name, id, legalPerson, registeredAddress, industryCode, businessLicenseUrl, businessLicenseId, contact, contactNumber }
      console.log(this.addForm)
    },
    // 提交表单
    async submit() {
      await this.$refs.ruleForm.validate()
      if (this.$route.query.id) {
        await updateEnterprise(this.addForm)
        this.$message.success('修改成功')
      } else {
        await addEnterprise(this.addForm)
        this.$message.success('添加成功')
      }
      this.$router.back()
    },
    // 重置表单
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.addForm = {
        name: '',
        legalPerson: '',
        registeredAddress: '',
        industryCode: '',
        contact: '',
        contactNumber: '',
        businessLicenseUrl: '',
        businessLicenseId: ''
      }
    }
  }
}
</script>

<style scoped lang="scss">
.add-enterprise {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
        display: flex;
        justify-content: space-between;
        padding: 0 20px;
        height: 64px;
        line-height: 64px;
        font-size: 16px;
        font-weight: bold;
        background-color: #fff;

        .left {
            span {
                margin-right: 4px;
            }

            .arrow {
                cursor: pointer;
            }
        }

        .right {
            text-align: right;
        }
    }

    .add-main {
        background: #f4f6f8;
        padding: 20px 130px;

        .form-container {
            background-color: #fff;

            .title {
                height: 60px;
                line-height: 60px;
                padding-left: 20px;
            }

            .form {
                margin-bottom: 20px;
                padding: 20px 65px 24px;

                .el-form {
                    display: flex;
                    flex-wrap: wrap;

                    .el-form-item {
                        width: 50%;
                    }
                }
            }
        }
    }

    .add-footer {
        position: fixed;
        bottom: 0;
        width: 100%;
        padding: 24px 50px;
        color: #000000d9;
        font-size: 14px;
        background: #fff;
        text-align: center;
    }
}
</style>
